﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using PCC.Framework;
using PCC.Framework.Utils;
using PCC.Framework.Biz;
using PCC.Framework.Model;

namespace PCC.Framework.JWeb.Handler
{
    /// <summary>
    /// ArchiveListHandler 的摘要说明
    /// </summary>
    public class ArchiveListHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            int pageIndex = 1;
            int pageSize = 10;
            if (!string.IsNullOrEmpty(context.Request.Form["page"]))
            {
                int.TryParse(context.Request.Form["page"], out pageIndex);
            }
            if (!string.IsNullOrEmpty(context.Request.Form["rows"]))
            {
                int.TryParse(context.Request.Form["rows"], out pageSize);
            }
            PageInfo pageInfo = new PageInfo
            {
             PageIndex = pageIndex,
             PageSize = pageSize
            };

            SortBy sortBy = null;

            if (!string.IsNullOrEmpty(context.Request.Form["sort"]) ||
                !string.IsNullOrEmpty(context.Request.Form["order"]))
            {
                sortBy = new SortBy
                {
                    SortName = context.Request.Form["sort"],
                    SortOrder = context.Request.Form["order"]
                };
            }

            ArchiveBiz archiveBiz = new ArchiveBiz();
            List<DocumentArchiveEntity> archiveList = null;
            archiveList = archiveBiz.QueryDocsByArchiveType(1,pageInfo,sortBy);
            
            string jsonStr = JsonHelper.ToJson(archiveList);

            var resultObj = new Dictionary<string,object>();
            resultObj.Add("total",pageInfo.RecordCount.ToString());
            resultObj.Add("rows", archiveList);

            jsonStr = JsonHelper.ToJson(resultObj);
            
            context.Response.ContentType = "text/plain";
            context.Response.Write(jsonStr);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}